/*************************************************************************
> File Name: test_01.cpp
> Author: hzy
> Created Time: 2023-08-14 19:21:37
> Description: 纯虚函数（例子：计算器的运算）
************************************************************************/
#include <iostream>

using namespace std;

class Calculator
{
protected:
    int a;
    int b;

public:
    virtual ~Calculator()
    {
        cout << "~Calculator()" << endl;
    }

public:
    void setA(int mya)
    {
        this->a = mya;
    }
    void setB(int myb)
    {
        this->b = myb;
    }

    virtual int getResult() = 0; // 声明为纯虚函数，在派生类中必须实现这个函数
};

// 实例化纯虚函数
int Calculator::getResult()
{
    return (a + b) * (a - b);
}

class Add : public Calculator
{
public:
    // override 关键字显式指示这是对基类虚函数的重写。
    int getResult()
    {
        return a + b;
    }
};

class Sub : public Calculator
{
public:
    int getResult()
    {
        return a - b;
    }
};

class Mul : public Calculator
{
public:
    int getResult()
    {
        return a * b;
    }
};

class Div : public Calculator
{
public:
    int getResult()
    {
        return a / b;
    }
};

class TT : public Calculator
{
public:
    int getResult()
    {
        int a = Calculator::getResult();

        return a;
    }
};

void doAction(Calculator *cal)
{
    cal->setA(5);
    cal->setB(2);

    cout << "结果为：" << cal->getResult() << endl;
    // delete cal;
}

void test()
{
    // Add add;
    // Sub sub;
    // Mul mul;
    // Div div;
    Calculator *add = new Add;
    Calculator *sub = new Sub;
    Calculator *mul = new Mul;
    Calculator *div = new Div;

    Calculator *t = new TT;
    doAction(t);

    cout << "加法";
    doAction(add);
    cout << "减法";
    doAction(sub);
    cout << "乘法";
    doAction(mul);
    cout << "除法";
    doAction(div);
}

int main()
{
    test();

    return 0;
}